#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    editorLayout_ = new QGridLayout(ui->centralWidget);
    editorLayout_->setContentsMargins(2,2,2,2);

    editorTabWidget_ = new EditorTabWidget();
    editorLayout_->addWidget(editorTabWidget_);

    editorTabWidget_->newEditor("untitled");
}

MainWindow::~MainWindow()
{
    delete editorTabWidget_;
    delete editorLayout_;
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *)
{
    while(editorTabWidget_->count() > 0)
        editorTabWidget_->closeTab(0);
}

void MainWindow::on_actionNew_file_triggered()
{
    editorTabWidget_->newEditor("untitled");
}

void MainWindow::on_actionOpen_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, "Open file...", QString(),
                       "HTML files (*.html *.htm);;Text files (*.txt)");

    if(filename.isEmpty())
        return;

    editorTabWidget_->newEditor(filename);
}

void MainWindow::on_actionSave_triggered()
{
    editorTabWidget_->saveCurrentFile();
}
